package entity

import (
	"context"
	"time"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/structure"
	"github.com/jinzhu/gorm"
)

// GetUserInfoDB 获取user_info存储
func GetUserInfoDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return GetDBWithModel(ctx, defDB, new(UserInfo))
}

// SchemaUserInfo user_info对象
type SchemaUserInfo schema.UserInfo

// ToUserInfo 转换为user_info实体
func (a SchemaUserInfo) ToUserInfo() *UserInfo {
	item := new(UserInfo)
	structure.Copy(a, item)
	return item
}

// UserInfo user_info实体
type UserInfo struct {
	ID           string     `gorm:"column:id;primary_key;size:36;"`
	HeadImg      string     `gorm:"column:head_img;size:255;"`                    // 头像
	Username     string     `gorm:"size:50;column:username;"`                     // 用户名
	RealName     string     `gorm:"size:50;column:real_name;"`                    // 真实姓名
	IsNewer      int        `gorm:"column:is_newer;size:11;type:tinyint;"`        // 新用户(1:新用户 2:老用户)
	InGroupTime  time.Time  `gorm:"type:datetime;column:in_group_time;size:255;"` // 入群时间
	AllReciteDay int        `gorm:"column:all_recite_day;size:11;"`               // 总背诵天数
	Level        int        `gorm:"type:tinyint;column:level;size:11;"`           // 用户等级
	LevelExpired time.Time  `gorm:"column:level_expired;size:255;type:datetime;"` // 过期时间
	Sex          int        `gorm:"type:tinyint;column:sex;size:11;"`             // 性别(0:保密 1:男 2:女)
	Phone        string     `gorm:"column:phone;size:20;"`                        // 手机号
	Intro        string     `gorm:"size:255;column:intro;"`                       // 个人介绍
	AccountNum   string     `gorm:"column:account_num;size:20;"`                  // 邀请码
	CodeImg      string     `gorm:"column:code_img;size:255;"`                    // 推荐二维码
	Pid          string     `gorm:"column:pid;size:36;"`                          // 上级用户ID
	Status       int        `gorm:"type:tinyint;column:status;size:11;"`          // 会员状态(1:正常 2:禁用)
	Creator      string     `gorm:"size:36;column:creator;"`                      // 创建者
	CreatedAt    time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt    time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt    *time.Time `gorm:"column:deleted_at;index;"`
	OpenId       string     `gorm:"column:open_id;size:255;"` // open_id
}

// ToSchemaUserInfo 转换为user_info对象
func (a UserInfo) ToSchemaUserInfo() *schema.UserInfo {
	item := new(schema.UserInfo)
	structure.Copy(a, item)
	return item
}

// UserInfos user_info列表
type UserInfos []*UserInfo

// ToSchemaUserInfos 转换为user_info对象列表
func (a UserInfos) ToSchemaUserInfos() []*schema.UserInfo {
	list := make([]*schema.UserInfo, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaUserInfo()
	}
	return list
}
